package com.o2opuzi.pub.service;

import java.util.Date;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.security.auth.login.LoginException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.o2opuzi.lib.db.Page;
import com.o2opuzi.pub.dao.UserDao;
import com.o2opuzi.pub.dao.UserLoginLogDao;
import com.o2opuzi.pub.dao.UserOperLogDao;
import com.o2opuzi.pub.model.User;
import com.o2opuzi.pub.model.UserLoginLog;
import com.o2opuzi.pub.model.UserOptLog;
import com.o2opuzi.pub.vo.UserLoginVo;

@Service
public class UserService {

	@Autowired
	private UserDao userDao;
	@Autowired
	private UserLoginLogDao userLoginLogDao;
	@Autowired
	private UserOperLogDao userOperLogDao;
	
	private ExecutorService executorService = Executors.newCachedThreadPool();

	/**
	 * 
	 * @auther Y.hj
	 * @param loginVo
	 * @throws LoginException
	 */
	@Transactional
	public User login(UserLoginVo loginVo) throws LoginException {
		User user = null;
		if (!loginVo.isIsadmin()) {
			// 普通用户
			user = userDao
					.findUser(loginVo.getAccount(), loginVo.getPassword());
		} else {
			// 管理员
			user = userDao.findAdminUser(loginVo.getAccount(),
					loginVo.getPassword());
		}
		if (user == null) {
			throw new LoginException("用户名或密码错误");
		}

		// 添加登录记录
		UserLoginLog loginLog = new UserLoginLog();
		loginLog.setLogin_client(loginVo.getClient());
		loginLog.setLogin_ip(loginVo.getIp());
		loginLog.setLogin_date(new Date());
		loginLog.setUser_id(user.getId());
		loginLog.setLogin_type(loginVo.getLogin_type());
		userLoginLogDao.save(loginLog);
		return user;

	}
	
	/**
	 * 获取用户登录日志
	 * @auther Y.hj
	 * @param user_id
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<Map<String, Object>> getUserLoginLogs(Integer user_id, Integer page,Integer size){
		Page<Map<String, Object>> data = userLoginLogDao.findPageMap(
				new UserLoginLog()
				.where()
				.eq("user_id", user_id)
				.orderByDesc("login_date"),
				page,
				size
		);
		return data;
	}
	
	/**
	 * 用户的操作记录
	 * @auther Y.hj
	 * @param user_id
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<UserOptLog> getUserOptLogs(Integer user_id,Integer page,Integer size){
		return userOperLogDao.findPage(new UserOptLog().where().eq("user_id", user_id), page, size);
	}
	

	/**
	 * 微信登录
	 * 
	 * @auther Y.hj
	 * @param openid
	 * @return
	 */
	@Transactional
	public User weixinLogin(String openid) {

		User user = userDao.findByWeixin(openid);
		if (user == null) {
			user = new User();
			user.setCreatedate(new Date());
			user.setWx_openid(openid);
			userDao.save(user);
		}
		//添加用户登录记录
		UserLoginLog loginLog = new UserLoginLog();
		loginLog.setLogin_date(new Date());
		loginLog.setUser_id(user.getId());
		loginLog.setLogin_type(UserLoginLog.LOGIN_TYPE_WX_AUTH);
		userLoginLogDao.save(loginLog);
		return user;

	}
	
	/**
	 * 添加用户操作日志 异步执行
	 * @auther Y.hj
	 * @param userOptLog
	 */
	public void addUserOptLog(final UserOptLog userOptLog) {
		executorService.execute(new Runnable() {
			@Override
			public void run() {
				userOperLogDao.save(userOptLog);
			}
		});
	}
}
